home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: delta / whiteline CD Series - delta.iso / vision / grafics / programm / img2ps / source / img2ps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-25  |  6.4 KB  |  286 lines

  1. /*
  2.     img2ps
  3.     
  4.         konversion von gem-ximage-dateien nach (encapsulated) postscript
  5.  
  6.     geschrieben von Th. Morus Walter
  7.  
  8.     (c) 1994/95 by Th. Morus Walter
  9.  
  10.     Änderungen:
  11.         v0.2:    erweitert für beliebige bildbreiten
  12.                 einfaches gem-user-interface
  13.                 runden der auflösung falls nahe vielfachem von 50 oder 90
  14. */
  15.  
  16. #include <aes.h>
  17. #include <tos.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22.  
  23. #include <image.h>
  24.  
  25. IMG_HEADER head;
  26. MFDB picture;
  27.  
  28. int use_gem=0;
  29.  
  30. void do_error(char *tos_error,char *gem_error)
  31. {
  32.     if ( use_gem ) {
  33.         form_alert(1,gem_error);
  34.         appl_exit();
  35.     }
  36.     else {
  37.         printf(tos_error);
  38.     }
  39.     exit(1);
  40. }
  41.  
  42. void write_out(FILE *file,char *str)
  43. {
  44. long len,out;
  45.  
  46.     len=strlen(str);
  47.  
  48.     out=fwrite(str,sizeof(char),len,file);
  49.  
  50.     if ( out!=len ) {
  51.         fclose(file);
  52.         do_error("i/o-Error, Disk full?\n","[1][ i/o-Error | Disk full?][Ok]");
  53.     }
  54.     if ( ferror(file) ) {
  55.         fclose(file);
  56.         do_error("i/o-Error\n","[1][ i/o-Error ][Ok]");
  57.     }
  58. }
  59.  
  60. void write_hexbyte(char *string,char byte)
  61. {
  62. char upper,lower;
  63.  
  64.     upper=(unsigned char)byte/0x10;
  65.     lower=byte&0x0F;
  66.  
  67.     *string++=upper<10?upper+'0':upper+'a'-10;
  68.     *string++=lower<10?lower+'0':lower+'a'-10;
  69.     *string=0;
  70. }
  71.  
  72. void do_output_data(FILE *file)
  73. {
  74. int i,j,k;
  75. char *l;
  76. char str[258];
  77.  
  78.     for ( i=0; i<picture.fd_h; i++ ) {
  79.         l=(char*)picture.fd_addr+picture.fd_wdwidth*2l*i;
  80.         for ( j=k=0; j<picture.fd_w; j+=8,k+=2,l++ ) {
  81.             write_hexbyte(str+k,~*l);
  82. /*            sprintf(str+k,"%02x",~*l);            */
  83.             if ( k==238 ) {            /* max. 240 zeichen/zeile */
  84.                 write_out(file,str);
  85.                 write_out(file,"\n");
  86.                 k=-2;
  87.             }
  88.         }
  89.         write_out(file,str);
  90.         write_out(file,"\n");
  91.     }
  92. }
  93.  
  94. double calc_rez(int pix_size)
  95. {
  96. double rez,rounded,error;
  97.  
  98.     rez=25400.0/pix_size;
  99.  
  100.     rounded=round(rez/50.0)*50.0;
  101.     error=fabs(rez-rounded)/rez;
  102.     if ( error<1e-2 )
  103.         return rounded;
  104.  
  105.     rounded=round(rez/90.0)*90.0;
  106.     error=fabs(rez-rounded)/rez;
  107.     if ( error<1e-2 )
  108.         return rounded;
  109.  
  110.     return rez;
  111. }
  112.  
  113. int main(int argc,char *argv[])
  114. {
  115. char in[256],out[256],*c;
  116. char puffer[512];
  117. FILE *file;
  118. double x,y,w,h;
  119.  
  120.     if ( argc==1 ) {        /* kein parameter übergeben */
  121.         if ( argv[0] )        /* name verfügbar? */
  122.             c=strrchr(argv[0],'.');
  123.             if ( !c || stricmp(c,".ttp") )
  124.                 use_gem=1;
  125.         else
  126.             use_gem=1;
  127.     }
  128.  
  129.     if ( !use_gem ) {
  130.         printf("img2ps V0.2 (%s) by Th. Morus Walter\n",__DATE__);
  131.         if ( argc<2 ) {
  132.             printf(" usage: img2ps infile outfile\n");
  133.             printf("     or img2ps infile\n");
  134.             printf(" thanx to John Bradley for XV\n\n");
  135.             return 1;
  136.         }
  137.         strcpy(in,argv[1]);
  138.     }
  139.     else {
  140.       char path[256],name[32];
  141.       int ret;
  142.  
  143.         appl_init();
  144.         path[0]=Dgetdrv()+'A';
  145.         path[1]=':';
  146.         path[2]='\\';
  147.         Dgetpath(path+2,0);
  148.         strcat(path,"\\*.IMG");
  149.         name[0]=0;
  150.         fsel_input(path,name,&ret);
  151.         if ( !ret ) {
  152.             appl_exit();
  153.             return 0;
  154.         }
  155.         strcpy(in,path);
  156.         c=strrchr(in,'\\');
  157.         if ( c )
  158.             *(c+1)=0;
  159.         strcat(in,name);
  160.     }
  161.  
  162.     if ( !use_gem )
  163.         printf("reading %s ...\n",in);
  164.  
  165.     if ( xload_img(&head,in,&picture)!=IMG_OK )
  166.         do_error("load-error: img2ps aborts\n","[1][Load-error| Program aborts... ][Ok]");
  167.  
  168.     if ( picture.fd_nplanes!=1 )
  169.         do_error("error: can't handle color images\n","[1][Error| can't handle color images... ][Ok]");
  170.  
  171.     if ( !use_gem && argc==3 )
  172.         strcpy(out,argv[2]);
  173.     else
  174.         strcpy(out,in);
  175.     c=strrchr(out,'.');
  176.     if ( c==0 )
  177.         strcat(out,".ps");
  178.     else
  179.         strcpy(c+1,"ps");
  180.  
  181.     if ( !use_gem )
  182.         printf("writing %s ...\n",out);
  183.  
  184.     w=(double)picture.fd_w/calc_rez(head.pix_wid)*72.0;
  185.     h=(double)picture.fd_h/calc_rez(head.pix_hght)*72.0;
  186.  
  187. /* alte fassung ohne korrektur von rundungsfehlern
  188.     w=picture.fd_w;
  189.     w*=head.pix_wid;
  190.     h=picture.fd_h;
  191.     h*=head.pix_hght;
  192.  
  193.     w/=25400.0/72.0;
  194.     h/=25400.0/72.0;
  195. */
  196. /*    zentriert auf A4-Seite
  197.     x=(612.0-w)/2;        /* 612/72" papierbreite */
  198.     y=(792.0-h)/2;        /* 792/72" papierhöhe */
  199. */
  200.     x=y=0.0;            /* links unten */
  201.  
  202.     file=fopen(out,"w");
  203.     if ( file ) {
  204.         write_out(file,"%!PS-Adobe-2.0 EPSF-2.0\n");
  205.         sprintf(puffer,"%%%%Title: %s\n",out);
  206.         write_out(file,puffer);
  207.         write_out(file,"%%Creator: img2ps V0.2 ("__DATE__") by Th. Morus Walter\n");
  208.         sprintf(puffer,"%%%%BoundingBox: %d %d %d %d\n",(int)(x+.5),(int)(y+.5),(int)(x+w+.5),(int)(y+h+.5));
  209.         write_out(file,puffer);
  210.         write_out(file,"%%Pages: 1\n");
  211.         write_out(file,"%%DocumentFonts:\n");
  212.         write_out(file,"%%EndComments\n");
  213.         write_out(file,"%%EndProlog\n");
  214.         write_out(file,"\n");
  215.         write_out(file,"%%Page: 1 1\n");
  216.         write_out(file,"\n");
  217.         write_out(file,"% remember original state\n");
  218.         write_out(file,"/origstate save def\n");
  219.         write_out(file,"\n");
  220.         write_out(file,"% build a temporary dictionary\n");
  221.         write_out(file,"20 dict begin\n");
  222.         write_out(file,"\n");
  223.         write_out(file,"% define string to hold a scanline's worth of data\n");
  224.         sprintf(puffer,"/pix %d string def\n",(picture.fd_w+7)/8);
  225.         write_out(file,puffer);
  226.         write_out(file,"\n");
  227.         write_out(file,"% lower left corner\n");
  228.         sprintf(puffer,"%d %d translate\n",(int)(x+.5),(int)(y+.5));
  229.         write_out(file,puffer);
  230.         write_out(file,"\n");
  231.         write_out(file,"% size of image (on paper, in 1/72inch coords)\n");
  232.         sprintf(puffer,"%lf %lf scale\n",w,h);
  233.         write_out(file,puffer);
  234.  
  235.         write_out(file,"\n");
  236.         write_out(file,"% dimensions of data\n");
  237.         sprintf(puffer,"%d %d %d\n",picture.fd_w,picture.fd_h,picture.fd_nplanes);
  238.         write_out(file,puffer);
  239.         write_out(file,"\n");
  240.         write_out(file,"% mapping matrix\n");
  241.         sprintf(puffer,"[%d %d %d %d %d %d]\n",picture.fd_w,0,0,-picture.fd_h,0,picture.fd_h);
  242.         write_out(file,puffer);
  243.         write_out(file,"\n");
  244.  
  245.         write_out(file,"{currentfile pix readhexstring pop}\n");
  246.         write_out(file,"image\n");
  247.  
  248.         do_output_data(file);
  249.  
  250.         write_out(file,"\n");
  251.         write_out(file,"showpage\n");
  252.  
  253.         write_out(file,"\n");
  254.         write_out(file,"% stop using temporary dictionary\n");
  255.         write_out(file,"end\n");
  256.  
  257.         write_out(file,"\n");
  258.         write_out(file,"% restore original state\n");
  259.         write_out(file,"origstate restore\n");
  260.  
  261.         write_out(file,"\n");
  262.         write_out(file,"%% Trailer\n");
  263.  
  264.         fclose(file);
  265.         if ( use_gem ) {
  266.           char alert[256],*f1,*f2;
  267.             f1=strrchr(in,'\\');
  268.             if ( f1 )    f1++;
  269.             else        f1=in;
  270.             f2=strrchr(out,'\\');
  271.             if ( f2 )    f2++;
  272.             else        f2=out;
  273.               sprintf(alert,"[1][IMG2PS| Converted| %s| to| %s][Ok]",f1,f2);
  274.               form_alert(1,alert);
  275.         }
  276.     }
  277.  
  278.     free(picture.fd_addr);
  279.  
  280.     if ( use_gem ) {
  281.         appl_exit();
  282.     }
  283.     return 0;
  284. }
  285.  
  286.